﻿var websocket = null;
if ('WebSocket' in window) {
    websocket = new WebSocket("ws://127.0.0.1:13528");
}
websocket.onopen = onOpen;
websocket.onmessage = onMessage;
websocket.onerror = onError;
websocket.onclose = onClose;

function onOpen(openEvt) {
    console.info(openEvt.Data);
}

function onMessage(evt) {
    var cmd = $.parseJSON(evt.data).cmd;
    if("getPrinters"===cmd){
        var tempString = "<option value=''></option>";
        $.each($.parseJSON(evt.data).printers,function(j,printer){
            tempString += "<option value='"+printer.name+"'>" + printer.name + "</option>";
        });

        $("#expressPrint").html(tempString);
        $("#expressPrint").trigger("chosen:updated");
    }else{
        console.info(evt.data);
    }
}
function onError() {}
function onClose() {}

function doSend() {
    if (websocket.readyState == websocket.OPEN) {
        var msg = document.getElementById("inputMsg").value;
        websocket.send(msg);//调用后台handleTextMessage方法
        alert("发送成功!");
    } else {
        alert("连接失败!");
    }
}

function doGetPrinters() {
    var request  = {
        requestID : '12345678901234567890',
        version : '1.0',
        cmd : 'getPrinters'
    };
    websocket.send(JSON.stringify(request));
}

function doPrinterConfig() {
    var request  = {
        requestID : "12345678901234567890",
        version : "1.0",
        cmd : "printerConfig"
    };
    websocket.send(JSON.stringify(request));
}

function doSetPrinterConfig() {
    var request  = {
        requestID : "12345678901234567890",
        version : "1.0",
        cmd : "setPrinterConfig",
        printer : {
            name : "打印机名称",
            needTopLogo : true,
            needBottomLogo: false
        }
    };
    websocket.send(JSON.stringify(request));
}

function doPrint(needExpressCompany){


}

window.close=function() {
    websocket.onclose();
}

function getLodop(oOBJECT,oEMBED){
}

function packFormDefaultPrinter(url) {
	var indexLength = getPrinterCount();
	var defalutPrintName=getPrinterName("-1");
	var tempString = "<option value='"+defalutPrintName+"'>"+defalutPrintName+"</option>";
	for ( var i = 0; i < indexLength; i++) {
		var name = getPrinterName(i);
		tempString += "<option value='"+name+"'>" + name + "</option>";
	}
	$("#"+url).html(tempString);
	$("#"+url).trigger("chosen:updated");
}
